package com.casic.modules.sys.service.impl;


import com.casic.common.annotation.DataFilter;
import com.casic.common.base.entity.PageEntity;
import com.casic.common.utils.Constant;
import com.casic.common.utils.ExpandHashMap;
import com.casic.modules.ddqiye.entity.QyxxDdEntity;
import com.casic.modules.ddqiye.service.QyxxDdService;
import com.casic.modules.sys.service.SysUserDeptService;
import com.casic.modules.sys.shiro.ShiroUtils;
import com.casic.modules.sys.entity.SysUserEntity;
import com.casic.modules.sys.dao.SysUserDao;
import com.casic.modules.sys.service.SysUserRoleService;
import com.casic.modules.sys.service.SysUserService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 系统用户
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年9月18日 上午9:46:09
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysUserDeptService sysUserDeptService;
	@Autowired
	private QyxxDdService qyxxDdService;

	/** 生成主键策略 */
	public String createId() {
		return UUID.randomUUID().toString().replaceAll("\\-", "");
	}

	@Override
	public List<String> queryAllPerms(String userId) {
		return sysUserDao.queryAllPerms(userId);
	}

	@Override
	public List<String> queryAllMenuId(String userId) {

		return sysUserDao.queryAllMenuId(userId);
	}

	@Override
	public SysUserEntity queryByUserName(String username) {
		List<String> roleIdList = sysUserDao.queryRoles(username);
		SysUserEntity sysUserEntity = sysUserDao.queryByUserName(username);
		sysUserEntity.setRoleIdList(roleIdList);
		return sysUserEntity;
	}
	
	@Override
	public SysUserEntity queryObject(String userId) {
		return sysUserDao.queryObject(userId);
	}

	@Override
	@DataFilter(tableAlias = "u", user = false)
	public List<SysUserEntity> queryList(Map<String, Object> map){
		return sysUserDao.queryList(map);
	}
	
	@Override
	@DataFilter(tableAlias = "u", user = false)
	public int queryTotal(Map<String, Object> map) {
		return sysUserDao.queryTotal(map);
	}

	@Override
	@Transactional
	public void save(SysUserEntity user) {
		String userId=createId();
		user.setUserId(userId);
		user.setCreateTime(new Date());
		//sha256加密
		String salt = RandomStringUtils.randomAlphanumeric(20);
		user.setSalt(salt);
		user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
		sysUserDao.save(user);
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
		//保存用户与部门关系
		sysUserDeptService.saveOrUpdate(user.getUserId(), user.getDeptIdList());
	}

	@Override
	@Transactional
	public void update(SysUserEntity user) {
		if(StringUtils.isBlank(user.getPassword())){
			user.setPassword(null);
		}else{
			user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
		}
		sysUserDao.update(user);
		
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
		//保存用户与部门关系
		sysUserDeptService.saveOrUpdate(user.getUserId(), user.getDeptIdList());
	}

	@Override
	@Transactional
	public void deleteBatch(String[] userId) {
		sysUserDao.deleteBatch(userId);
		sysUserRoleService.deleteBatch(userId);
		sysUserDeptService.deleteBatch(userId);
	}

	@Override
	public int updatePassword(String userId, String password, String newPassword) {
		Map<String, Object> map = new HashMap<>();
		map.put("userId", userId);
		map.put("password", password);
		map.put("newPassword", newPassword);
		return sysUserDao.updatePassword(map);
	}

	@Override
	public List<SysUserEntity> findByDeptId(String deptId) {
		return sysUserDao.selectByDeptId(deptId);
	}

	@Override
	public PageEntity<SysUserEntity> findListParams(Map<String, Object> params) {
		int currentPage=1;
		int pageSize=10;
		if(params.get("page")!=null){
			currentPage=Integer.parseInt((String)params.get("page"));
		}
		if(params.get("limit")!=null){
			pageSize=Integer.parseInt((String)params.get("limit"));
		}
		params.put("deleteState", Constant.DELETE_STATE_NO);
		PageHelper.startPage(currentPage, pageSize);
		List<SysUserEntity> list=sysUserDao.selectListParams(params);
		return new PageEntity<SysUserEntity>(list);
	}

	@Override
	public SysUserEntity queryActiveByUserName(String username) {
		return sysUserDao.queryActiveByUserName(username);
	}

	@Override
	public void updateLastLoginTime(SysUserEntity user) {
		if(user.getLastLoginTime() == null){
			user.setLastLoginTime(new Date());
		}
		sysUserDao.updateLastLoginTime(user);
	}

	@Override
	public List<String> queryRoles(String username) {
		return sysUserDao.queryRoles(username);
	}
}
